<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/3
 * Time: 14:10
 */

namespace pay;


use think\Loader;

class Ali
{
    // 接口调用地址
    public $url = 'https://openapi.alipay.com/gateway.do';
    
    // 商户appId
    public $appId;
    
    // 支付宝公钥 上传应用公钥后 支付宝生成的支付宝公钥
    public $alipayrsaPublicKey;
    
    // 应用私钥
    public $rsaPrivateKey;
    
    public function __construct($config = [])
    {
        $this->appId = $config['appId'];
        $this->alipayrsaPublicKey = $config['alipayrsaPublicKey'];
        $this->rsaPrivateKey = $config['rsaPrivateKey'];
    }
    
    /**
     * 支付宝退款
     * @param $money   退款金额
     * @param $num     订单号
     */
    public function aliReturn($money, $num)
    {
        //仨参数：money,number
        import('alipay.aop.AopClient', EXTEND_PATH);
        import('alipay.aop.request.AlipayTradeRefundRequest', EXTEND_PATH);
        $aop = new \AopClient;
        $aop->gatewayUrl = $this->url;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayTradeRefundRequest;
//        $refundAmount = $money;
        $refundAmount = 0.01;
        $request->setBizContent("{" .
            "\"out_trade_no\":\"" . $num . "\"," .
            "\"trade_no\":\"\"," .
            "\"refund_amount\":" . $refundAmount . "," .
            "\"refund_reason\":\"退款\"" .
            "  }");
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode) && $resultCode == 10000) {
            return array('status' => 200, 'msg' => '操作成功');
        } else {
            return array('status' => 0, 'msg' => '调用接口失败');
        }
    }
    
    /**
     * 阿里提现
     * @param $data    参数数组
     * @return array
     */
    public function aliTiXian($data){
        $out_biz_no = $data['out_biz_no']; // 订单号
        $payee_account = $data['payee_account']; // 支付宝账号
        $amount = $data['amount']; // 交易金额
        $payee_real_name = $data['payee_real_name']; // 真实姓名
        // 引入文件
        // require_once(EXTEND_PATH.'alipay/AopSdk.php');
        Loader::import('alipay.aop.AopClient',EXTEND_PATH,'.php');
        Loader::import('alipay.aop.request.AlipayFundTransToaccountTransferRequest',EXTEND_PATH,'.php');
        // Loader::import('alipay.aop.SignData',EXTEND_PATH,'.php');// 在AopClient类中已经引用，此处无序重复引用
        $aop = new \AopClient();
        // 支付宝网关，固定不变
        $aop->gatewayUrl = $this->url;
        // appId 应用的ID
        $aop->appId = $this->appId;
        // 私钥，工具生成
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        // 支付宝公钥 上传应用公钥后 支付宝生成的支付宝公钥
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        // 版本  固定
        $aop->apiVersion = '1.0';
        // 签名类型  固定
        $aop->signType = 'RSA2';
        // 编码  固定
        $aop->postCharset = 'UTF-8';
        // 格式 固定
        $aop->format = 'json';
        $request = new \AlipayFundTransToaccountTransferRequest();
        // 收款方账户类型为（payee_type）：
        // 1.ALIPAY_LOGONID：支付宝登录号，支持邮箱和手机号格式。
        // 2.ALIPAY_USERID：支付宝账号对应的支付宝唯一用户号。以2088开头的16位纯数字组成。
        $request->setBizContent("{" .
            "\"out_biz_no\":\"$out_biz_no\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"$payee_account\"," .
            "\"amount\":\"$amount\"," .
            "\"payee_real_name\":\"$payee_real_name\"," .
            "}");
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode) && $resultCode == 10000){
            $success = ['status' => 200, 'msg' => ''];
        }else{
            // $result->$responseNode->sub_msg 这个参数 是返回的错误信息
            $success = ['status' => 0, 'msg' => $result->$responseNode->sub_msg];
        }
        return $success;
    }
}